
/**
 * Created by lx on 2015-09-24
 */

$(function() {

   $('#ticketForm').validate();
   //初始化频道列表
   initChannelData();
   //初始化专辑选择效果
   fun_videoSelectId();
    
});
/**
 * 初始化频道列表
 */
function initChannelData(){
    $.get("/vrs/channel/listAll.jsn",function(data,textStatus){
        if(textStatus == "success"){
            var success = data.success;
            if(success){
                var optionList = new Array();
                optionList[0]='<option value="">全部</option>';
                $.each(data.data,function(i,item){
                    optionList[parseInt(i+1)]='<option value="'+item.channelId+'">'+item.name+'</option>';
                });
                var $newSelect = $('<select id="channelSelect"  data-placeholder="请选择频道" class="chosen w200"  >'+optionList.join()+'</select>');
                var $select =  $('#channelSelect');
                $select.next().remove();
                $select.replaceWith($newSelect);
                $('.chosen').chosen();
                fun_albumSelectId();
            }
        }
    },"json");
}

/**
 * 初始化专辑列表
 */
function initAlbumData(){
    //专辑列表
    $.get("/vrs/album/listAll.jsn?isdisplay=1",function(data,textStatus){
        if(textStatus == "success"){
            var success = data.success;
            if(success){
                var optionList = new Array();
                optionList[0]='<option value=""></option>';
                $.each(data.data,function(i,item){
                    optionList[parseInt(i+1)]='<option value="'+item.albumId+'">'+item.name+'</option>';
                });
                var $newSelect = $('<select id="albumSelect" required data-placeholder="请选择专辑" class="chosen w200" name="ticket.albumId">'+optionList.join()+'</select>');
                var $select =  $('#albumSelect');
                $select.next().remove();
                $select.replaceWith($newSelect);
                $('.chosen').chosen();
                fun_videoSelectId();
            }
        }
    },"json");
}

/**
 * 选择专辑效果
 */
function fun_albumSelectId(){
	
    /**选择频道过滤专辑*/
	$('#channelSelect').change(function(){
        var value = $('#channelSelect').val();
        var url = "/vrs/album/listJson.jsn?channelId="+value;
        if(value == ''){
        	url = "/vrs/album/listAll.jsn";
        }
        //加载等待
        var $newSelect1 = $('<select id="albumSelect" required data-placeholder="加载中..." class="chosen w200" name="ticket.albumId"></select>');
        $('#albumSelect').next().remove();
        $('#albumSelect').replaceWith($newSelect1);
        $('.chosen').chosen();
        
        $.ajax({
            type: "get",
            dataType: "json",
            url: url,
            error: function () {

            },
            success: function (data) {
                var success = data.success;
                if(success){
                    var rows = data.data;
                    var optionList = new Array();
                    optionList[0]='<option value=""></option>';
                    $.each(rows,function(i,item){
                        optionList[parseInt(i+1)]='<option value="'+item.albumId+'">'+item.name+'</option>';
                    });
                    var $newSelect = $('<select id="albumSelect" required data-placeholder="请选择专辑" class="chosen w200" name="ticket.albumId">'+optionList.join()+'</select>');
                    var $select =  $('#albumSelect');
                    $select.next().remove();
                    $select.replaceWith($newSelect);
                    
                  //加载等待
            	    var $newSelect1 = $('<select id="videoIdSelect" required multiple data-placeholder="请先选择专辑" class="chosen w200" name="ticket.videoIds"></select>');
            	    $('#videoIdSelect').next().remove();
            	    $('#videoIdSelect').replaceWith($newSelect1);
                    
                    $('.chosen').chosen();
                    fun_videoSelectId();
                }
            }
     });
	});
}
/**
 * 选择视频效果
 */
function fun_videoSelectId(){
	/**选择专辑过滤视频*/    
	$('#albumSelect').change(function(){
	    var value = $('#albumSelect').val();
	    var url = "/vrs/video/listJson.jsn?video.album.albumId="+value;
	    if(value == ''){
	        return;
	    }
	    
	    //加载等待
	    var $newSelect1 = $('<select id="videoIdSelect" required multiple data-placeholder="加载中..." class="chosen w200" name="ticket.videoIds"></select>');
	    $('#videoIdSelect').next().remove();
	    $('#videoIdSelect').replaceWith($newSelect1);
        $('.chosen').chosen();
        
	    $.ajax({
	        type: "get",
	        dataType: "json",
	        url: url,
	        error: function () {
	
	        },
	        success: function (data) {
	            var success = data.success;
	            if(success){
	                var rows = data.data.rows;
	                var optionList = new Array();
	                optionList[0]='<option value=""></option>';
	                $.each(rows,function(i,item){
	                    optionList[parseInt(i+1)]='<option value="'+item.videoId+'">'+item.name+'</option>';
	                });
	                var $newSelect = $('<select id="videoIdSelect" required multiple data-placeholder="请选择视频" class="chosen w200" name="ticket.videoIds">'+optionList.join()+'</select>');
	                var $select =  $('#videoIdSelect');
	                $select.next().remove();
	                $select.replaceWith($newSelect);
	                $('.chosen').chosen();
	            }
	        }
	    });
	});
}








